$(document).ready(function() {
	loadGridData("");
	$('#dg').datagrid({
		toolbar : toolbar,
		rownumbers : true,
		singleSelect : true
	});

});

function loadGridData(filter) {
	var userId = $("#userId").val();
	if (userId == undefined)
		return;
	$.getJSON('listActionItems.htm?userId=' + userId + '&filter=' + filter,
			function(resData) {
				$('#dg').datagrid({
					data : resData
				});
			});
}
var toolbar = [ {
	id : 'btnClaim',
	text : 'Claim',
	iconCls : 'icon-add',
	handler : claimOpportunity
}, {
	id : 'btnApprove',
	text : 'Approve',
	iconCls : 'icon-ok',
	handler : approveOpportunity
}, {
	id : 'btnReject',
	text : 'Reject',
	iconCls : 'icon-cancel',
	handler : rejectOpportunity
}, '-', {
	id : 'btnMyClaims',
	text : 'My Claims',
	iconCls : 'icon-inbox-plus',
	handler : getClaimedByMe
}, {
	id : 'btnAllOpportunity',
	text : 'All Opportunity',
	iconCls : 'icon-archives',
	handler : getAllOpportunity
}, '-', {
	id : 'btnViewDetails',
	text : 'View Details',
	iconCls : 'icon-search',
	handler : viewOpportunityDetails
}


];

function viewOpportunityDetails() {
	var row = $('#dg').datagrid('getSelected');
	if (row == null) {
		showMessager("Info", "Please select an opportunity to view details");
		return;
	}
	if(row.stepId == "6" || row.stepId == 6){
		var win = window.open("ResourceDetails.htm?assId=" + row.id
				+ "&opportunityId=" + row.opportunityId + "&prjObjId="
				+ row.prjObjId,
				"width=880,height=750,scrollbars=yes,location=no,menubar=no");
		win.focus();
	}else if(row.stepId == "5" || row.stepId == 5){
		
	}
	else{
		var win = window.open("ViewOpportunity.htm?assId=" + row.id
				+ "&opportunityId=" + row.opportunityId + "&prjObjId="
				+ row.prjObjId,
				"width=880,height=750,scrollbars=yes,location=no,menubar=no");
		win.focus();
	}
}

function getOpportunityDetails() {
	var row = $('#dg').datagrid('getSelected');
	if (row == null) {
		showMessager("Info", "Please select an opportunity to view details");
		return;
	}

	var url = "getDetail.htm?opportunityId=" + row.opportunityId;

	$.ajax({
		url : url
	});
}

function gridReset() {
	loadGridData('Pending');
}
function getClaimedByMe() {
	loadGridData('Claimed');
}
function getAllOpportunity() {
	loadGridData('Pending');
}
function claimOpportunity() {
	var row = $('#dg').datagrid('getSelected');
	if (row == null) {
		showMessager("Error", "Please select an Opportunity to claim!");
		return false;
	}

	if ((row.stepId == "2" || row.stepId == "3" || row.stepId == "4" || row.stepId == "5" || row.stepId == "6")	&& row.status == "Pending") {

	} else {
		showMessager("Error", "Only pending opportunity can be claimed!");
		return false;
	}
	var data = {
		"opportunityId" : row.opportunityId,
		"id" : row.id,
		"userId" : $("#userId").val(),
		"action" : "claim",
		"pmuser" : "None"
	};

	$.postJSON("OpportunityAction.htm", data, function(result) {
		if (result.success) {
			showMessager("Info", result.message);
			loadGridData('Pending');
		} else {
			showMessager("Exception", result.message);
		}
	}, function(xhr, ajaxOptions, thrownError) {
		showMessager("Exception", thrownError);
	});
}


function approveOpportunity() {
	var row = $('#dg').datagrid('getSelected');
	
	if (row == null) {
		showMessager("Error", "Please select an Opportunity to approve!");
		return false;
	}
	if (row.status != "Claimed") {
		showMessager("Error", "Only claimed opportunity can be approved!");
		return false;
	}
	if (row.userId != $("#userId").val()) {
		showMessager("Error",
				"You can not approve any opportunity claimed by others !!!");
		return false;
	}
	
	if (row.stepId == "4" || row.stepId == 4 ) {
		$('#prcApprovalWindow').dialog('open');
		return false;
	}
	
	
	if (row.stepId == "2") {
		openApprovalWindow();
		return false;
	}
	
	var data = {
		"opportunityId" : row.opportunityId,
		"id" : row.id,
		"userId" : $("#userId").val(),
		"action" : "approve",
		"pmuser" : "None"
	};

	$.postJSON("OpportunityAction.htm", data, function(result) {
		if (result.success) {
			showMessager("Info", result.message);
			loadGridData('Pending');
		} else {
			showMessager("Exception", result.message);
		}
	}, function(xhr, ajaxOptions, thrownError) {
		showMessager("Exception", thrownError);
	});
}

function approvePRCOpportunity() {
	if (!$("#approvalForm").form('validate')) {
		showMessager("Alert", 'Required fields can not be left blank.');
		return false;
	}
	
	var row = $('#dg').datagrid('getSelected');
	var pmuser=$("#showPM").combobox('getValue');
	if (row == null) {
		showMessager("Error", "Please select an Opportunity to approve!");
		return false;
	}
	if (row.status != "Claimed") {
		showMessager("Error", "Only claimed opportunity can be approved!");
		return false;
	}
	if (row.userId != $("#userId").val()) {
		showMessager("Error",
				"You can not approve any opportunity claimed by others !!!");
		return false;
	}
	
	
	
	var data = {
		"opportunityId" : row.opportunityId,
		"id" : row.id,
		"userId" : $("#userId").val(),
		"action" : "approve",
		"pmuser" : pmuser
	};

	$.postJSON("OpportunityAction.htm", data, function(result) {
		if (result.success) {
			showMessager("Info", result.message);
			loadGridData('Pending');
			$('#prcApprovalWindow').dialog('close');	
		} else {
			showMessager("Exception", result.message);
		}
	}, function(xhr, ajaxOptions, thrownError) {
		showMessager("Exception", thrownError);
	});
}


function rejectOpportunity() {
	var row = $('#dg').datagrid('getSelected');
	if (row == null) {
		showMessager("Error", "Please select an Opportunity to reject!");
		return false;
	}
	
	var data = {
		"opportunityId" : row.opportunityId,
		"id" : row.id,
		"userId" : $("#userId").val(),
		"action" : "reject",
		"pmuser" : "None"
	};

	$.postJSON("OpportunityAction.htm", data, function(result) {
		if (result.success) {
			showMessager("Info", result.message);
			loadGridData('Pending');
		} else {
			showMessager("Exception", result.message);
		}
	}, function(xhr, ajaxOptions, thrownError) {
		showMessager("Exception", thrownError);
	});
}

//function rejectOpportunity() {
//	var row = $('#dg').datagrid('getSelected');
//	if (row == null) {
//		showMessager("Error", "Please select an Opportunity to reject!");
//		return false;
//	}
//	if ((row.stepId == "3" || row.stepId == "4" || row.stepId == "6") && row.status == "Claimed") {
//
//	} else if (row.stepId == "2" && row.status == "Claimed") {
//		showMessager("Error", "You cannot reject the Opportunity!");
//		return false;
//	} else {
//		showMessager("Error", "Only claimed opportunity can be rejected!");
//		return false;
//	}
//	
//	
//	var data = {
//		"opportunityId" : row.opportunityId,
//		"id" : row.id,
//		"userId" : $("#userId").val(),
//		"action" : "reject",
//		"pmuser" : "None"
//	};
//
//	$.postJSON("OpportunityAction.htm", data, function(result) {
//		if (result.success) {
//			showMessager("Info", result.message);
//			loadGridData('Pending');
//		} else {
//			showMessager("Exception", result.message);
//		}
//	}, function(xhr, ajaxOptions, thrownError) {
//		showMessager("Exception", thrownError);
//	});
//}


function openApprovalWindow() {
	var row = $('#dg').datagrid('getSelected');
	if (row == null) {
		showMessager("Error", "Please select an Opportunity to approve!");
		return false;
	}
	if (row.status != "Claimed") {
		showMessager("Error", "Only Claimed opportunity can be approved!");
		return false;
	}

	if (row.userId != $("#userId").val()) {
		showMessager("Error",
				"You can not approve any opportunity claimed by others!");
		return false;
	}

	var type = "";
	if (row.role == "FC")
		type = "financial";
	else if (row.role == "CA")
		type = "legal";
	else if (row.role == "PMO")
		type = "strategic";
	else if (row.role == "PMORM")
		type = "capability";
	else if (row.role == "BD")
		type = "BD";
	

	var url = "OppReview.htm?type=" + type + "&userId=" + $("#userId").val()
			+ "&opportunityId=" + row.opportunityId + "&prjObjId="
			+ row.prjObjId + "&assignmentId=" + row.id;
	window.open(url, "popupWindow",
			"width=880,height=750,scrollbars=yes,location=no,menubar=no");
	return false;

}
